var feature = $('#feature');
var sYear=$('#syear');
var sMonth = $('#smonth');
var sDay = $('#sday');
var sHour = $('#shour');
var eYear=$('#eyear');
var eMonth = $('#emonth');
var eDay = $('#eday');
var eHour = $('#ehour');
var search = $('#search');
var stVal='';
var fanVal='';
var featureId='';
var sMonthVal='';
var tableBox=$('.dataShow');
var back=$('#return');
var idName=getStore('idName');
var featureName=getStore('feature');
var chartBox=$('#mychart');


//表头数据
var featureData = [
	{
		name: 'pba',
		value: '风机性能评估',
		header:{
			start_time:'开始时间',
			end_time:'结束时间',
			ltime:'数据丢失时间（小时）',
			pba:'PBA',
			agp:'实际发电量（kW）',
			pgp:'潜在发电量',
			sgp:'应发电量（kW）',
			loss:'损失发电量',
			ploss:'性能损失',
			odgp:'停机损失发电量',
			lpgp:'限电损失发电量',
			ontime:'发电时间',
			offtime:'停机时间',
			lptime:'限电时间',
			tba:'TBA',
			minws:'最小风速',
			maxws:'最大风速',
			mws:'风速均值'
		}
	}, {
		name: 'blade',
		value: '叶片结冰',
		header:{
			start_time: '开始时间',
			end_time: '结束时间',
			alarm: '结果',
			Prob: '叶片结冰概率'
		}
	}, {
		name: 'yaw',
		value: '偏航对风不正',
		header:{
			start_time:'开始时间',
			end_time:'结束时间',
			/*run_time:'不显示',*/
			max_dir_3:'误差角度',
			rank:'准确等级',
			alarm:'预警',
		}
	}, {
		name: 'generator',
		value: '发电机温度异常',
		header:{
			datetime:'时间戳',
			distance:'向量距离',
			vec_std:'风险值',
			alarm:'预警'
		}
	}, {
		name: 'anemometer',
		value: '测风仪异常',
		header:{/*
			ws_diff:'不显示',
			gp_diff:'不显示',*/
			start_time:'开始时间',
			end_time:'结束时间',
			alarm:'预警',
		}
	}, {
		name: 'generatorb',
		value: '发电机轴承卡滞',
		header:{/*
			ws_diff:'不显示',
			gp_diff:'不显示',*/
			start_time:'开始时间',
			end_time:'结束时间',
			alarm:'预警'
		}
	}, {
		name: 'bladep',
		value: '叶片污染',
		header:{/*
			ws_diff:'不显示',
			gp_diff:'不显示',*/
			start_time :'开始时间',
			end_time :'结束时间',
			alarm :'预警'
		}
	},{
		name: 'pbadev',
		value: '功率曲线偏移',
		header:{
			'user_def_mid':'机组号',
			start_time:'开始时间',
			end_time:'结束时间',
			power_curve_dev_mean:'功率曲线偏移程度',
			norm_test:'功率曲线图形异常'	
		}
	},{
		name: 'pitch',
		value: '变桨电机温度异常',
		header:{
			endtime:'时间戳',
			result12:'变桨电机1、2温度异常',
			result13:'变桨电机1、3温度异常',
			result23:'变桨电机2、3温度异常',
			alarm:'预警'	
		}
	}
];

for (var i = 0; i < featureData.length; i++) {
	if(featureData[i].name==featureName.enName){
		featureData[i].selected='selected';
	}else{
		delete featureData[i].selected;
	}
}

//风机特征下拉
setDropData(feature,featureData);
//月
setDropNum(sMonth,12);
setDropNum(eMonth,12);
//小时
setDropNum(sHour,24);
setDropNum(eHour,24);
//日
sMonth.on('change', function() {
	var _this=this;
	changeDay(_this,sDay,)
});
eMonth.on('change', function() {
	var _this=this;
	changeDay(_this,eDay)
});



/*var header=$('#tableheader');
//内容
var tbody=$('#tablebody');*/
var tbodyTr='';

var timeStamp=[];

//加载的数据
search.on('click', function() {
	var featureVal=feature.val();
	var headerJSON=JSONfindArr(featureData,'name',featureVal,'header');
	chartBox.hide();
	if(featureVal=='pba'){
		$('.tip').show();
	}else{
		$('.tip').hide();
	}
	tableBox.html('');
	$.ajax({
		url:URL,
		type: 'POST',
		data: {
			method:feature.val(),
			stid:idName.stVal,
			mid:idName.fanVal,
			starttime:sYear.val()+'-'+toDou(sMonth.val())+'-'+toDou(sDay.val())+' '+toDou(sHour.val())+':00:00',
			endtime:eYear.val()+'-'+toDou(eMonth.val())+'-'+toDou(eDay.val())+' '+toDou(eHour.val())+':00:00',
			type:0
		},
		success: function(data) {
			if(data==null || data==''){
				alert('no data');
				return false;
			}
			var result=eval('('+data+')');
			var showTable=new ViewTable(tableBox,headerJSON,result);
			//需要返回timeStamp
			timeStamp=showTable.loadBody2(featureVal);
		},
		error: function(e) {
			console.log(e);
		}
	})
});


//实例化图表
var summaryChart=new Chart('mychart');
tableBox.on('click','.contain tr',function(){
	var index=$(this).index();
	if(timeStamp.length==0){
		return false;
	}else{
		chartBox.show();
		$.ajax({
			url:URL,
			type:'POST',
			data:{
				method:feature.val(),
				stid:idName.stVal,
				mid:idName.fanVal,
				type:1,
				querytime:timeStamp[index]
			},
			success:function(data){
				var res=eval('('+data+')');
				var dataArr=[];
				var dataArr2=[];
				for(var i=0;i<res[0].GP.length;i++){
					dataArr[i]=[res[0].WSbin[i],res[0].GP[i]];
				}
				for(var i=0;i<res[1].GP.length;i++){
					dataArr2[i]=[res[1].WSbin[i],res[1].GP[i]];
				}

				summaryChart.dataArr=dataArr;
				summaryChart.dataArr2=dataArr2;
				summaryChart.loadData();

			},
			error:function(e){
				console.log(e);
			}
		})
	}

});

back.on('click',locationDetail);

